package com.malljelly.study.service.impl;

import com.malljelly.study.dao.mapper.UnionPayMapper;
import com.malljelly.study.dao.po.PaymentInfo;
import com.malljelly.study.dao.po.PaymentType;
import com.malljelly.study.dao.repository.PaymentInfoRepository;
import com.malljelly.study.dao.repository.PaymentTypeRepository;
import com.malljelly.study.redis.BaseRedisService;
import com.malljelly.study.service.PayAdaptService;
import com.malljelly.study.service.UnionPayService;
import com.malljelly.study.trans.PaymentInfoPoToPaymentInfoVo;
import com.malljelly.study.trans.PaymentInfoVoToPaymentInfoPo;
import com.malljelly.study.vo.unionpay.service.UnionPaymentInfoRequestVo;
import com.malljelly.study.vo.unionpay.service.UnionPaymentInfoResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Optional;
import java.util.UUID;



/**
 * Created by 骑着蜗牛去旅行 on 2018/8/14.
 */
@Service
@Slf4j
public class UnionPayServiceImpl implements UnionPayService {

   /* @Autowired
    private UnionPayMapper unionPayMapper;*/

    @Autowired
    private PaymentInfoRepository paymentInfoRepository;

    @Autowired
    private PaymentTypeRepository paymentTypeRepository;


    @Autowired
    private YinLianPayImpl yinLianPay;

    @Autowired
    private BaseRedisService baseRedisService;

    /**
     * 1.创建支付请求token
     *
     */
    @Override
    public String addPayInfoToken(UnionPaymentInfoRequestVo paymentInfo) {
        PaymentInfo po = new PaymentInfoVoToPaymentInfoPo().apply(paymentInfo);

        PaymentInfo info = paymentInfoRepository.save(po);

        //spring的断言工具
        Assert.notNull(info.getId(),"系统错误,为获取到支付id");
        String token = "pay-" + UUID.randomUUID().toString();

        //存入redis并设置过期时间
        baseRedisService.set(token,info.getId(),3000L);
        return token;
    }

    /**
     * 2.使用token查找支付信息
     *
     */
    @Override
    public UnionPaymentInfoResponseVo getPayInfoByToken(String token) {

        //spring的断言工具
        Assert.notNull(token,"token不能为空");

        String payInfoId =  baseRedisService.get(token).toString();
        /**
         * java8的非空判断
         */
        Optional<PaymentInfo> po = paymentInfoRepository.findById(Long.parseLong(payInfoId));
        return  new PaymentInfoPoToPaymentInfoVo().apply(po.get());
    }
    /**
     * 3.使用订单号查询支付信息
     *
     */
    @Override
    public UnionPaymentInfoResponseVo getByOrderIdPayInfo(String orderId) {

        /**
         * java8的非空判断 和lambda表达式
         */
        Optional<PaymentInfo> po = paymentInfoRepository.findOne((root, query, cb) -> cb.equal(root.get("orderId").as(String.class), orderId));

        return  new PaymentInfoPoToPaymentInfoVo().apply(po.get());
    }

    /**
     * 4.修改订单状态
     *
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updatePayInfo(UnionPaymentInfoRequestVo paymentInfo) {
        PaymentInfo po = new PaymentInfoVoToPaymentInfoPo().apply(paymentInfo);
        paymentInfoRepository.saveAndFlush(po);
        return true;
    }

    @Override
    public String pay(UnionPaymentInfoResponseVo paymentInfo) {

        //1根据id获取支付类型
        Optional<PaymentType> paymentType = paymentTypeRepository.findById(paymentInfo.getTypeId());
        if (paymentType == null) {
            log.error("支付类型是空的");
            return null;
        }

        //2获取支付名称 进去适配器的匹配
        String typeName = paymentType.get().getTypeName();
        PayAdaptService payAdaptService=null;
        switch (typeName) {
            case "yinlianPay":
                payAdaptService=yinLianPay;
                break;
            default:
                break;
        }
        return payAdaptService.pay(paymentInfo, paymentType.get());
    }
}
